package com.blood.common.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yelong
 * @date 2020/11/4 15:13
 * @description
 * @Copyright
 */
public class Tools {
    /**
     * id拼接
     *
     * @param ids
     * @return
     */
    public static String listIds(List<String> ids) {
        StringBuffer sb = new StringBuffer();
        if (ids != null && ids.size() > 0) {
            sb.append("'");
            ids.forEach(i -> {
                if (sb.length() > 1) {
                    sb.append("','");
                }
                sb.append(i);
            });
            sb.append("'");
        }
        return sb.toString();
    }

    /**
     * id拼接
     *
     * @param ids
     * @return
     */
    public static String listInteger(List<Integer> ids) {
        StringBuffer sb = new StringBuffer();
        if (ids != null && ids.size() > 0) {
            ids.forEach(i -> {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(i);
            });
        }
        return sb.toString();
    }

    /**
     * 构建唯一的文件名
     *
     * @return
     */
    public static String getFileName() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /**
     * 通过身份证号码获取出生日期、性别、年龄
     *
     * @param certificateNo
     * @return 返回的出生日期格式：yyyy-MM-dd
     */
    public static Map<String, String> getBirAgeSex(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag) {
                    return new HashMap<String, String>();
                }
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag) {
                    return new HashMap<String, String>();
                }
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8) + "-"
                    + certificateNo.substring(8, 10) + "-"
                    + certificateNo.substring(10, 12);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? "女" : "男";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "-"
                    + certificateNo.substring(10, 12) + "-"
                    + certificateNo.substring(12, 14);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "女" : "男";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;
    }

    /**
     * 获取2个list 相同的数据
     *
     * @param s
     * @param s1
     * @return
     */
    public static List<String> getIdenticalList(List<String> s, List<String> s1) {
        List<String> ss = new ArrayList<>();
        for (String s2 : s1) {
            if (s.contains(s2)) {
                ss.add(s2);
            }
        }
        return ss;
    }


    /**
     * 判断文件大小
     *
     * @param len  文件长度
     * @param size 限制大小
     * @param unit 限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(Long len, int size, String unit) {
//        long len = file.length();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }


    /**
     * 去除重复元素
     *
     * @param list
     * @return
     */
    public static List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }
}
